/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.log.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.log.entity.LogService;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.modules.log.vo.ServiceLogVO;
import org.springblade.modules.log.wrapper.ServiceLogWrapper;
import org.springblade.modules.log.service.IServiceLogService;
import org.springblade.core.boot.ctrl.BladeController;
import springfox.documentation.annotations.ApiIgnore;
import java.util.Map;

/**
 * 业务日志表 控制器
 *
 * @author junxin
 * @since 2023-04-08
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-serviceLog/serviceLog")
@Api(value = "业务日志表", tags = "业务日志表接口")
public class ServiceLogController extends BladeController {

	private final IServiceLogService serviceLogService;

	/**
	 * 业务日志表 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入serviceLog")
	public R<ServiceLogVO> detail(LogService serviceLog) {
		LogService detail = serviceLogService.getOne(Condition.getQueryWrapper(serviceLog));
		return R.data(ServiceLogWrapper.build().entityVO(detail));
	}
	/**
	 * 业务日志表 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入serviceLog")
	public R<IPage<ServiceLogVO>> list(@ApiIgnore @RequestParam Map<String, Object> serviceLog, Query query) {
		IPage<LogService> pages = serviceLogService.page(Condition.getPage(query), Condition.getQueryWrapper(serviceLog, LogService.class));
		return R.data(ServiceLogWrapper.build().pageVO(pages));
	}

	/**
	 * 业务日志表 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入serviceLog")
	public R<IPage<ServiceLogVO>> page(ServiceLogVO serviceLog, Query query) {
		IPage<ServiceLogVO> pages = serviceLogService.selectServiceLogPage(Condition.getPage(query), serviceLog);
		return R.data(pages);
	}

	/**
	 * 业务日志表 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入serviceLog")
	public R save(@Valid @RequestBody LogService serviceLog) {
		return R.status(serviceLogService.save(serviceLog));
	}

	/**
	 * 业务日志表 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入serviceLog")
	public R update(@Valid @RequestBody LogService serviceLog) {
		return R.status(serviceLogService.updateById(serviceLog));
	}


	/**
	 * 业务日志表 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(serviceLogService.removeByIds(Func.toLongList(ids)));
	}

	/**
	 * 业务日志表 举报
	 */
	@PostMapping("/report")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "举报操作日志", notes = "传入id")
	public R report(@ApiParam(value = "主键", required = true) @RequestParam String id) {
		LogService entity = serviceLogService.getById(id);
		if (entity.getStatus()==0) throw new ServiceException("已经是被举报状态");
		entity.setStatus(0);//设置为被举报状态、
		return R.status(serviceLogService.updateById(entity));
	}



}
